import os
import sys
import ctypes
import time
import psutil
from subprocess import call

home_dir = os.getcwd()               #获得当前路径

choice_list = ['上传','下载']

def git_update():
    os.chdir(home_dir)
    git_add_cmd    = "git add ."
    git_commit_cmd = "git commit -m {}".format(gitdate)
    git_push_cmd   = "git push origin master"
    call(
       git_add_cmd + "&&" +
       git_commit_cmd + "&&" +
       git_push_cmd,
       shell=True
    )

def git_get():
    os.chdir(home_dir)
    git_pull_cmd = "git pull origin master"
    call(
        git_pull_cmd,
        shell=True
    )

def is_process_running(process_name):
    pl = psutil.pids()
    for pid in pl:
        if psutil.Process(pid).name() == process_name:
            return True
    else:
        return False

if __name__ == "__main__":

    ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 6)
    print("---------------------------------------------------")
    print("欢迎使用 SiYuan Auto Backup,poweredBy kingstacker!")
    print("---------------------------------------------------")
    print("程序当前运行路径:")
    print(home_dir)
    time.sleep(0.5)
    print("----------------")
    print("检查程序执行路径是否正确中...")
    dir_status = int(os.path.exists(home_dir+'\\data'))
    if dir_status == 1:
        print("程序执行路径正确.")
        pass
    else:
        print("未发现当前路径存在data文件夹,请确认!")
        os.system("pause")
        sys.exit()

    eth_exit_code = 1
    print("----------------")
    print('检查网络连接状态中，请等待...')
    status_befor = 0;
    auto_pull_status = 0;
    while True:
        if eth_exit_code == 1:
            # eth_exit_code = int(os.system('ping www.baidu.com > /dev/null'))
            eth_exit_code = int(os.system('ping www.baidu.com'))

        if eth_exit_code == 1:
            print('没联网，确认你的网络连接状态.')

        # time.sleep(2)
        while True:
            gitdate = time.strftime("%Y-%m-%d/%H-%M-%S/%A", time.localtime())
            if eth_exit_code == 1:
                break
            # if auto_pull_status == 0:
            #     print("----------------")
            #     print("拉取远程文件中...")
            #     git_get()
            #     print("拉取远程文件完成!")
            #     print("----------------")
            # time.sleep(2)

            # try:
            #     siyuan_program_status = int(is_process_running("SiYuan.exe"))  # 查看思源软件是否打开
            # except Exception as e:
            #     print e
            # else:
            #     pass
            # if status_befor == 0 and siyuan_program_status == 1:
            #     print("----------------")
            #     print("发现思源软件已打开，等待软件关闭...")
            # if status_befor == 1 and siyuan_program_status == 0:
                # print("----------------")
                # print("发现思源软件已关闭，备份笔记到云端中...")
                # print("请等待...")
            print("拉取远程文件更新确认中...")
            git_get()
            print("正在提交笔记...")
            git_update()
            print("提交备份已完成!")
            print("当前备份时间点:",gitdate)
            print("----------------")
            # status_befor = int(siyuan_program_status)
            # auto_pull_status = 1;



